﻿using System;
using System.Data;

using HsTec.Mobile.Green.BLL;
using System.Web.UI.WebControls;
using HsTec.Mobile.Green.Util;


public partial class DataStorage_DataStorageQuery : System.Web.UI.Page
{
    DataStorageBLL bll = new DataStorageBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((string)Session["Baimasoft_Pers_ID"] == null)
        {
            Response.Redirect("../Logon.aspx");
        }
        //绑定指标选项下拉框
        lstItems.Attributes.Add("onclick", "FindSelectedItems(this," + txtItems.ClientID + ");");
        if (!IsPostBack)
        {
            if (ViewState["sortDirection"] == null)
            {
                ViewState["sortDirection"] = " ASC";
            }
            if (ViewState["sortExpression"] == null)
            {
                ViewState["sortExpression"] = String.Empty;
            }
            BindsltArea();
        }
    }
    protected void ddlDataTable_SelectedIndexChanged(object sender, EventArgs e)
    {
        string sValue = ddlDataTable.SelectedValue.Trim();
        if (!String.IsNullOrEmpty(sValue))
        {
            txtItems.Value = String.Empty;
            lstItems.Items.Clear();
            lstItems.DataSource = bll.GetItemsList(sValue);
            lstItems.DataTextField = "descript";
            lstItems.DataValueField = "colname";
            lstItems.DataBind();
        }
    }

    private string GetSearchString()
    {
        string searchStr = "(1=1)";
        string sddlRegionid = sltArea.SelectedValue.ToString().Trim();
        if (sddlRegionid != "")
        {
            searchStr += " and Area_ID  ='" + sddlRegionid + "' ";
        }
        if (this.txtSTime.Text.ToString().Trim() != "")
        {
            searchStr += " and Years >= substring(replace(convert(varchar(10),'" + this.txtSTime.Text.ToString().Trim() + "',120),'-',''),0,7) ";
        }
        //else
        //{
        //    searchStr += " and Years >= substring(replace(convert(varchar(10),'" + DateTime.Now.ToString("yyyy-MM-dd") + "',120),'-',''),0,7) ";
        //}
        if (this.txtETime.Text.ToString().Trim() != "")
        {
            searchStr += "and Years <= substring(replace(convert(varchar(10),'" + this.txtETime.Text.ToString().Trim() + "',120),'-',''),0,7) ";
        }
        //else
        //{
        //    searchStr += " and Years <= substring(replace(convert(varchar(10),'" + DateTime.Now.ToString("yyyy-MM-dd") + "',120),'-',''),0,7)";
        //}
        return searchStr;
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string sValue = ddlDataTable.SelectedValue.Trim();
        if (!String.IsNullOrEmpty(sValue))
        {
            BindGridViewData();
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string sValue = ddlDataTable.SelectedValue.Trim();
        if (!String.IsNullOrEmpty(sValue) && !String.IsNullOrEmpty(HiddenID.Value))
        {
            bll.Delete(HiddenID.Value.TrimEnd(','), sValue);
            BindGridViewData();
        }
    }
    protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[1].Visible = false; //如果想使第1列不可见,则将它的可见性设为false
            e.Row.Cells[2].Width = 50;
            e.Row.Cells[3].Width = 50;
            e.Row.Cells[4].Width = 50;
        }
    }

    private void BindsltArea()
    {
        sltArea.DataTextField = "Name";
        sltArea.DataValueField = "ID";
        sltArea.DataSource = bll.GetAreaList();
        sltArea.DataBind();
        sltArea.Items.Insert(0, "全省");
        sltArea.Items[0].Value = "";
    }

    private void BindGridViewData()
    {
        string sValue = ddlDataTable.SelectedValue.Trim();
        if (!String.IsNullOrEmpty(sValue))
        {
            string sItems = String.Empty;
            for (int i = 0; i < lstItems.Items.Count; i++)
            {
                if (lstItems.Items[i].Selected == true)
                {
                    sItems += "," + lstItems.Items[i].Value + " AS [" + lstItems.Items[i].Text + "]";
                }
            }
            DataView mydv = bll.GetReportList(sValue, GetSearchString(), sItems);
            if (!String.IsNullOrEmpty(ViewState["sortExpression"].ToString()))
                mydv.Sort = ViewState["sortExpression"].ToString() + ViewState["sortDirection"].ToString();
            GridView1.DataSource = mydv;
            GridView1.DataBind();
            HiddenID.Value = "";
        }
    }
    protected void btnAudti_Click(object sender, EventArgs e)
    {
        string sValue = ddlDataTable.SelectedValue.Trim();
        if (!String.IsNullOrEmpty(sValue) && !String.IsNullOrEmpty(HiddenID.Value))
        {
            bll.Audit(HiddenID.Value.TrimEnd(','), sValue, "1");
            BindGridViewData();
        }
    }
    protected void btnUnAudit_Click(object sender, EventArgs e)
    {
        string sValue = ddlDataTable.SelectedValue.Trim();
        if (!String.IsNullOrEmpty(sValue) && !String.IsNullOrEmpty(HiddenID.Value))
        {
            bll.Audit(HiddenID.Value.TrimEnd(','), sValue, "0");
            BindGridViewData();
        }
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["sortDirection"].ToString() == " ASC")
        {
            ViewState["sortDirection"] = " DESC";
        }
        else
        {
            ViewState["sortDirection"] = " ASC";
        }
        ViewState["sortExpression"] = e.SortExpression;
        this.BindGridViewData();
    }
}
